package io.gitee.chearnee.fw.dingtalk.robot;

import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import io.gitee.chearnee.fw.dingtalk.enums.AppType;
import io.gitee.chearnee.fw.dingtalk.enums.MsgTypeEnum;
import io.gitee.chearnee.fw.dingtalk.manager.DingTalk;
import io.gitee.chearnee.fw.dingtalk.property.DingTalkProperties;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/7/28 21:26
 */
@Component
public class DingTalkRobot extends DingTalk {
    private static final String SPLIT = "@";

    public DingTalkRobot() {
        super(AppType.ROBOT);
    }

    @Override
    protected DingTalkClient getClient(String businessUri) {
        return super.dingTalkClient(this.appType, businessUri);
    }

    @SneakyThrows
    private OapiRobotSendResponse send(OapiRobotSendRequest request, String name) {
        if (StringUtils.hasText(name)) {
            setDingTalkName(name);
        }
        DingTalkProperties.DingTalkConfig property = dingTalkUtil.getProperty(dingTalkNameThreadLocal.get());
        if (property == null || !property.isEnable()) {
            return new OapiRobotSendResponse();
        }
        return sendResponse(getClient("/send").execute(request));
    }

    public boolean sendText(String text, OapiRobotSendRequest.At at) {
        return sendText(text, at, null);
    }

    public boolean sendText(String text, OapiRobotSendRequest.At at, String name) {
        OapiRobotSendRequest.Text robotText = new OapiRobotSendRequest.Text();
        robotText.setContent(text);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setText(robotText);
        request.setMsgtype(MsgTypeEnum.TEXT.getCode());

        request.setAt(at);
        OapiRobotSendResponse response = send(request, name);
        return response.isSuccess();
    }

    public boolean sendMarkdown(String title, String text, OapiRobotSendRequest.At at) {
        if (!CollectionUtils.isEmpty(at.getAtMobiles())) {
            StringBuilder stringBuilder = new StringBuilder(text).append("\n");
            for (String atMobile : at.getAtMobiles()) {
                stringBuilder.append(SPLIT).append(atMobile);
            }
            text = stringBuilder.toString();
        }
        return sendMarkdown(title, text, at, null);
    }

    public boolean sendMarkdown(String title, String text, OapiRobotSendRequest.At at, String name) {
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle(title);
        markdown.setText(text);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMarkdown(markdown);
        request.setMsgtype(MsgTypeEnum.MARKDOWN.getCode());
        request.setAt(at);
        OapiRobotSendResponse response = send(request, name);
        return response.isSuccess();
    }

    public boolean sendLink(String title, String text, String picUrl, String messageUrl,
                            OapiRobotSendRequest.At at) {
        return sendLink(title, text, picUrl, messageUrl, at, null);
    }

    public boolean sendLink(String title, String text, String picUrl, String messageUrl,
                            OapiRobotSendRequest.At at, String name) {
        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setTitle(title);
        link.setText(text);
        link.setMessageUrl(messageUrl);
        link.setPicUrl(picUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setLink(link);
        request.setMsgtype(MsgTypeEnum.LINK.getCode());
        request.setAt(at);
        OapiRobotSendResponse response = send(request, name);
        return response.isSuccess();
    }
}
